{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a1978948",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['22%', '333%', '4444%']\n"
     ]
    }
   ],
   "source": [
    "def rate(*numbers,base = 100):\n",
    "    l = list()\n",
    "    for i in numbers:\n",
    "        l.append(\"{}%\".format(i))\n",
    "    return(l) #有返回值\n",
    "l = rate(22,333,4444) #就要有接收返回值 即l=\n",
    "print(l)\n",
    "\n",
    "val = exec(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c7b7d157",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x 3\n",
      "y 4\n",
      "z 5\n",
      "fighter1 Fedor\n",
      "fighter2 Randleman\n"
     ]
    }
   ],
   "source": [
    "def traverse_kargs(a=5,**kwargs): #可以传默认参数，但k不能与传入可变参数的k一样，否则会认为传入的不是可变参数，也就是说这里a换成y，\n",
    "                                   # y 4就不会当成可变参数kwargs被输出\n",
    "    for k, v in kwargs.items():\n",
    "        print(k, v)\n",
    "traverse_kargs(x=3, y=4, z=5)       # 依次打印('x', 3), ('y', 4), ('z', 5)\n",
    "traverse_kargs(fighter1='Fedor', fighter2='Randleman')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "487e4403",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2\n",
      "(4, 5)\n",
      "{'a': 6, 'b': 'bar'}\n"
     ]
    }
   ],
   "source": [
    "def foo(x, y, c = 10,*args, **kwargs):#在函数里加入一个默认参数，视加入的位置不同，假如在可变参tuple前，会顶掉tuple中的一个元素。\n",
    "                                      # 假如默认参数与dict中的k相同且在dict可变参数之前，则会顶替dict中的一个元素\n",
    "    print(x, y) \n",
    "    print(args)\n",
    "    print(kwargs)\n",
    "\n",
    "# 第一个pring输出(1, 2)\n",
    "# 第二个print输出(3, 4, 5)\n",
    "# 第三个print输出{'a': 3, 'b': 'bar'}\n",
    "foo(1, 2, 3, 4, 5, a=6, b='bar') #传入的dict k不能与普通参数相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3a2e5711",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3456 1 642.5384615384615\n",
      "5467846577 3576 1525757573.25\n",
      "3 1 2.0\n"
     ]
    }
   ],
   "source": [
    "def max_min_average(*arr):\n",
    "    assert(len(arr) >= 1) #为了防止传入一个空tuple，规定arr长度>=1\n",
    "    max = arr[0]\n",
    "    min = arr[0]\n",
    "    sum = 0\n",
    "    for i in arr:\n",
    "        if i >= max:\n",
    "            max = i\n",
    "        if i <= min:\n",
    "            min = i\n",
    "        sum += i\n",
    "    avg = sum/len(arr)\n",
    "    # return(max,min,avg)\n",
    "    return max,min,avg #返回值可以不加括号，有三个返回值就要有三个接收的值\n",
    "a,b,c = max_min_average(1,2,4,325,26,43,6,3456,3456,64,56,457,457) #这里接收的符号可以与返回的不一样，也可以一样，用哪三个都行\n",
    "print(a,b,c)\n",
    "x,y,z = max_min_average(5467846577,634756564,3576,423576)\n",
    "print(x,y,z)\n",
    "max,min,avg = max_min_average(1,2,3)\n",
    "print(max,min,avg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "92463a91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, 18\n",
      "100\n",
      "200\n",
      "3, 200\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "#全局变量:子函数外边定的变量，叫做全局变量\n",
    "#所有的函数都可以使用它的值\n",
    "#如果函数需要修改全局变量的值，那么需要在这个函数中，使用 global xxx进行说明\n",
    "age = 18\n",
    "def A():\n",
    "    age = 100\n",
    "    print(age)\n",
    "def B():\n",
    "    global age\n",
    "    age = 200\n",
    "    print(age)   \n",
    "def C():\n",
    "    print(age)\n",
    "   \n",
    "if __name__ == \"__main__\": #程序入口，可以理解为一个定义函数和调用函数的分界线\n",
    "    print('1,',age) \n",
    "    A()\n",
    "    B()\n",
    "    print('3,',age)\n",
    "    C()\n",
    "else:\n",
    "    print(\"未进入main程序\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d6e6dab",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
